var STip = ZFT.Util.STip;
var ParseTemplate = ZFT.Util.ParseTemplate;
var Ajax = ZFT.Util.Ajax;
var Template = {
    balance: ParseTemplate(require("../tpl/balance.xtpl")),
    setpwdmodal: ParseTemplate(require("../tpl/blancesetpwd.xtpl")),
    nextstep: ParseTemplate(require("../tpl/accinfostep.xtpl"))
}
var Balance = ZFT.Util.Class({
    container: $("body"),
    __canNext: true,
    __canLoad: true,
    timer: null,
    RESEND_VCODE_TIME: 120,
    init: function () {

    },
    EVENTS: {
        "click #accBalance": "loadBalance",
        "click #wdBtn": "checkWDCondition",
        "click .wd.u-bankcrd_box": "toggleBankBackground"
    },
    loadBalance: function (e) {
        var _this = this;
        var tar = (e) ? $(e.currentTarget) : $("#accBalance");
        if (tar.hasClass("active") && _this.__canLoad == false) return false;
        tar.addClass("active").siblings().removeClass("active");
        Ajax("/r/Member_getMemberMoney", {
            type: "POST",
            dataType: "json",
            success: function (res) {
                if (res.code == 200) {
                    var html = Template.balance({ data: res.data });
                    $("#centerContent").html(html);
                    _this.__canLoad = false;
                } else {
                    return STip("fail", res.msg);
                }
            }
        })
    },
    checkWDCondition: function (e) {
        var regCode = /^\d{6}$/;
        var balance = $(e.currentTarget).attr("data-bl");
        var _this = this;
        var withDrawBox = $("#withDrawBox");
        Ajax("/r/Member_getAccountInfo", {
            type: "POST",
            dataType: "json",
            success: function (res) {
                var data = res.data
                if (res.code == 200) {
                    if (res.data.withdraw_pwd == 0) {
                        var html;
                        data["type"] = 1;
                        html = Template.setpwdmodal({ data: data });
                        withDrawBox.html(html).show(200);
                        _this.clearChromeAutoFix();
                        $("#nextAddBtn").on("click", function (e) {
                            var code = $("#codeInp").val();
                            var addPwdStepBox = $("#addPwdStepBox");
                            if (!regCode.test(code)) return STip("fail", "请输入6位数字验证码！");
                            if (!_this.__canNext) return false;
                            Ajax("/r/VerifyCode_checkWithdrawPwdCode", {
                                type: "POST",
                                dataType: "json",
                                params: {
                                    mobile_code: code
                                },
                                success: function (res) {
                                    if (res.code == 200) {
                                        var type = {
                                            type: 1
                                        }
                                        var html = Template.nextstep({ data: type });
                                        addPwdStepBox.html(html);
                                        _this.clearChromeAutoFix();  //清除chrome自动填充黄色背景
                                        setTimeout(function () {
                                            $("#wdInp,#pwdInp").val("").css("background-color", "#fff");
                                            $("input:-webkit-autofill").css("-webkit-box-shadow", "0 0 0 1000px white inset");
                                            $("#wdFee,#wdRealFee").text(0);
                                        }, 500);
                                        $("#savePwdBtn").on("click", function () {
                                            var newPwd = $("#newPwd").val();
                                            var confPwd = $("#confPwd").val();
                                            if (!regCode.test(newPwd) || !regCode.test(confPwd)) return STip("fail", "提现密码为6位数字！");
                                            Ajax("/r/Member_setWithdrawPwd", {
                                                type: "POST",
                                                dataType: "json",
                                                params: {
                                                    new_pwd: newPwd,
                                                    confirm_pwd: confPwd
                                                },
                                                success: function (res) {
                                                    if (res.code == 200) {
                                                        STip("success", res.msg);
                                                        withDrawBox.html("");
                                                        _this.__canLoad = true;
                                                        _this.loadBalance()
                                                    } else {
                                                        return STip("fail", res.msg);
                                                    }
                                                }
                                            })
                                        })
                                    }
                                }
                            })
                        })

                    } else {
                        var box;
                        var regBlane = /^(([1-9]\d{0,11}))(\.\d{1,2})?$/;  //正则匹配金额                      
                        var feeDetail; //手续费
                        data["bl"] = balance;
                        balance = parseFloat(balance);
                        box = Template.setpwdmodal({ data: data });
                        withDrawBox.html(box).show(200);
                         _this.clearChromeAutoFix();  //清除chrome自动填充黄色背景
                        $("#wdInp").on("change", function (e) {
                            var tarInp = $(e.currentTarget);
                            var tarVal = parseFloat(tarInp.val());

                            if (regBlane.test(tarVal) && tarVal > 1) {

                                if (tarVal > balance) {
                                    tarInp.val(balance);
                                    tarVal = balance;
                                }
                                feeDetail = (tarVal * 0.005).toFixed(2);
                                $("#wdFee").text((feeDetail > 1) ? feeDetail : 1);
                                $("#wdRealFee").text(((tarVal > balance ? balance : tarVal) - ((feeDetail > 1) ? feeDetail : 1)).toFixed(2));
                               // console.log(feeDetail)
                            } else {
                                tarInp.val("");
                                $("#pwdInp").val("");
                                $("#wdFee,#wdRealFee").text(0);
                                return STip("fail", "提现金额大于1，小数点保留2位！");
                            }
                        });
                        $("#subWdBtn").on("click", function (e) {
                            var money = $("#wdInp").val();
                            var choosedBank = $(".u-bankcrd_box.pick");
                            var type = choosedBank.attr("data-type");
                            var account = choosedBank.attr("data-card");
                            var pwd = $("#pwdInp").val();
                            var param;
                            console.log(money)
                            if (choosedBank.length < 1) return STip("fail", "请选择一个提现账号！");
                            if (!regBlane.test(money)) return STip("fail", "请输入正确的金额，小数点保留2位！");
                            if (!regCode.test(pwd)) return STip("fail", "提现密码格式错误！");
                            param = {
                                money: money,
                                account_type: type,
                                account: account,
                                withdraw_pwd: hex_md5(hex_md5(pwd))
                            }
                            Ajax("/r/Withdraw_withdrawApply", {
                                type: "POST",
                                dataType: "json",
                                params: param,
                                loading: function () {
                                    $("#subWdBtn").removeClass("m-btn_primary").attr("disabled", "disabled");
                                },
                                success: function (res) {
                                    if (res.code == 200) {

                                        STip("success", res.msg, 4000);
                                        window.location.href = "?t=2";
                                    } else {
                                        $("#subWdBtn").addClass("m-btn_primary").removeAttr("disabled");
                                        return STip("fail", res.msg);
                                    }
                                }
                            })
                        })


                    }
                }
            }
        })
    },
    //清除chrome自动填写 黄色背景
    clearChromeAutoFix: function () {
        setTimeout(function () {
            $("#wdInp,#pwdInp").val("").css("background-color", "#fff");
            $("input:-webkit-autofill").css("-webkit-box-shadow", "0 0 0 1000px white inset");
            $("#wdFee,#wdRealFee").text(0);
        }, 300);
    },
    //选中银行卡边框
    toggleBankBackground: function (e) {
        var tarBtn = $(e.currentTarget);
        tarBtn.addClass("pick").siblings().removeClass("pick");
    }

})

$(function () {
    new Balance();
})